package com.mls.fivedistrict.controller.portal;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Dynamic;
import com.mls.fivedistrict.server.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 动态 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-07-06
 */
@RestController("portalDynamicController")
@RequestMapping("/api/app")
public class DynamicController extends BaseController {
    @Autowired
    DynamicService dynamicService;

    @GetMapping("/v1/dynamic/{dynamicId}")
    public ServiceResponse getDynamic(@PathVariable Long dynamicId) {
        Optional.ofNullable(dynamicId).orElseThrow(() -> new CustomerException("动态id不能为空"));
        Dynamic dynamic = dynamicService.selectById(dynamicId);
        if (null != dynamic) {
            return ServiceResponse.createSuccessResponse(dynamic);
        }
        return ServiceResponse.createErrorResponse("查无该动态");
    }

    @GetMapping("/v1/dynamics")
    public ServiceResponse getDynamicList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List selectList = dynamicService.selectList(pageFilter.getWrapper());
        PageInfo<Dynamic> dynamicPageInfo = new PageInfo<Dynamic>(selectList, 5);
        return ServiceResponse.createSuccessResponse(dynamicPageInfo);
    }
}

